package com.zzy.demo.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.zzy.demo.dao.PersonDao;
import com.zzy.demo.model.Person;


@Component
public class MyUserDetailsService implements UserDetailsService{

    private static final Logger LOGGER = LoggerFactory.getLogger(MyUserDetailsService.class);
    
    @Autowired
    private PersonDao personDao;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LOGGER.info("表单登录用户名：{}", username);
        //从数据库读取用户
        Person person=personDao.findByUsername(username);
        
        String password=person.getPassword();
          
        LOGGER.info("数据库密码：{}", password);        
        
        return new User(username, 
        		password,
        		true,   //可用
        		true,   //账户过期
        		true,  //认证过期
        		true, //锁定用户
        		AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
    }

}
